from flask import jsonify
from flask_cors import cross_origin
from flask_restful import Resource

from database import Database


class GetArticleById(Resource):
    @cross_origin(origin='*', headers=['Content-Type', 'Authorization'])
    def get(self, articleId):
        db = Database()
        articles = db.execute_query("SELECT * FROM article WHERE articleId = %s", (articleId,))

        print(articles)
        for article in articles:
            article_json = {
                "articleId": article[0],
                "type": article[1],
                "publisher": article[2],
                "title": article[3],
                "subtitle": article[4],
                "content": article[5],
                "imageUrl": article[6],
                "date": article[7].strftime("%Y-%m-%d %H:%M:%S")
            }
            return jsonify(article_json)
        else:
            return {"message": "Article not found"}, 404
